
/*
 * =====================================================================================
 *
 *       Filename:  Statistics.cc
 *
 *    Description:

 *
 *        Version:  1.0
 *        Created:  12/06/19 11:51:01
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  XXXXXXX (), XXXX@jd.com
 *        Company:  JDAI
 *        Copyright:  Copyright 2019, XXXXXXX
 *
 * =====================================================================================
 */

#include "Statistics.h"

static bvar::Adder < int >g_request_counter;    //请求次数
static bvar::Window < bvar::Adder < int >>g_request_counter_minute("tts", "request_count_minute", &g_request_counter, 60);  //单位时间内请求的次数

static bvar::LatencyRecorder g_first_request_latency("tts", "first_request", 60);   //首包时延

static bvar::Adder < int >g_first_request_counter;  //首包统计个数， 应该是没有实际意义与g_request_counter有点重复， 先保留这把
static bvar::Window < bvar::Adder < int >>g_first_request_counter_minute("tts", "first_request_count_minute", &g_first_request_counter, 60);

static bvar::Adder < int >g_error_request_cntl;
static bvar::Window < bvar::Adder < int >>g_error_request_cntl_minute("tts", "ERROR_REQUEST_CNTL", &g_error_request_cntl, 60);

static bvar::Adder < int >g_error_server_busy;
static bvar::Window < bvar::Adder < int >>g_error_server_busy_minute("tts", "ERROR_SERVER_BUSY", &g_error_server_busy, 60);

static bvar::Adder < int >g_error_parameter;
static bvar::Window < bvar::Adder < int >>g_error_parameter_minute("tts", "ERROR_PARAMETER", &g_error_parameter, 60);

static bvar::Adder < int >g_error_text_too_short;
static bvar::Window < bvar::Adder < int >>g_error_text_too_short_minute("tts", "ERROR_TEXT_TOO_SHORT", &g_error_text_too_short, 60);

static bvar::Adder < int >g_error_text_too_long;
static bvar::Window < bvar::Adder < int >>g_error_text_too_long_minute("tts", "ERROR_TEXT_TOO_LONG", &g_error_text_too_long, 60);

static bvar::Adder < int >g_error_seqid_for_session;
static bvar::Window < bvar::Adder < int >>g_error_seqid_for_session_minute("tts", "ERROR_SEQID_FOR_SESSION", &g_error_seqid_for_session, 60);

static bvar::Adder < int >g_error_tts_read_response_failed;
static bvar::Window < bvar::Adder < int >>g_error_tts_read_response_failed_minute("tts", "ERROR_TTS_READ_RESPONSE_FAILED", &g_error_tts_read_response_failed, 60);

/**
 * @brief 请求次数
 */
void Statistics::addTotalReqCount() {
    g_first_request_counter << 1;
}

void Statistics::addFirstReqCount() {
    g_first_request_counter << 1;
}

/**
 * @brief 首包时延
 *
 * @param tc
 */
void Statistics::addFirsLatency(struct timeval& lastTime) {
    struct timeval now;
    gettimeofday(&now, NULL);
    int ms = ((now.tv_sec - lastTime.tv_sec) * 1000000 + (now.tv_usec - lastTime.tv_usec)) / 1000;
    g_first_request_latency << ms;
    lastTime = now;
}

void Statistics::addError(int& err) {
    switch (err) {
    case ERROR_REQUEST_CNTL:
        g_error_request_cntl << 1;
        break;

    case ERROR_SERVER_BUSY:
        g_error_server_busy << 1;
        break;

    case ERROR_PARAMETER:
        g_error_parameter << 1;
        break;

    case ERROR_TEXT_TOO_SHORT:
        g_error_text_too_short << 1;
        break;

    case ERROR_TEXT_TOO_LONG:
        g_error_text_too_long << 1;
        break;

    case ERROR_SEQID_FOR_SESSION:
        g_error_seqid_for_session << 1;
        break;

    case ERROR_TTS_READ_RESPONSE_FAILED:
        g_error_tts_read_response_failed << 1;
        break;

    default:
        break;
    }
}
